package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysAttachment;
import com.ruoyi.system.service.ISysAttachmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 附件管理Controller
 *
 * @author shuyan
 * @date 2021-01-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/attachment")
public class SysAttachmentController extends BaseController {

    private final ISysAttachmentService iSysAttachmentService;

    /**
     * 查询附件管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysAttachment sysAttachment) {
        startPage();
        List<SysAttachment> list = iSysAttachmentService.queryList(sysAttachment);
        return getDataTable(list);
    }

    /**
     * 导出附件管理列表
     */
    @GetMapping("/export")
    public AjaxResult export(SysAttachment sysAttachment) {
        List<SysAttachment> list = iSysAttachmentService.queryList(sysAttachment);
        ExcelUtil<SysAttachment> util = new ExcelUtil<SysAttachment>(SysAttachment.class);
        return util.exportExcel(list, "attachment");
    }

    /**
     * 获取附件管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iSysAttachmentService.getById(id));
    }

    /**
     * 上传附件
     */
    @PostMapping("/uploadAttachment")
    public AjaxResult uploadAttachment(SysAttachment sysAttachment, MultipartFile file) {
        AjaxResult ajaxResult = toAjax(iSysAttachmentService.saveAttachment(sysAttachment, file));
        ajaxResult.put("data", sysAttachment);
        return ajaxResult;
    }

    /**
     * 删除附件
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iSysAttachmentService.removeAttachment(Arrays.asList(ids)));
    }

    /**
     * 根据文件路径删除
     */
    @DeleteMapping("/delAttachmentByFilePath")
    public AjaxResult delAttachmentByFilePath(String filePath) {
        iSysAttachmentService.delAttachmentByFilePath(filePath);
        return AjaxResult.success("操作完成");
    }

    /**
     * 根据文件路径获取详情
     *
     * @param filePath 文件路径
     */
    @GetMapping("/getAttachmentByFilePath")
    public AjaxResult getAttachmentByFilePath(String filePath) {
        return AjaxResult.success(iSysAttachmentService.getAttachmentByFilePath(filePath));
    }
}
